package cn.com.elite.workorder;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ContextHelper {
	private static FileSystemXmlApplicationContext _ctx;

	/*
	����ClassPathXmlApplicationContext()��classpath: ǰ׺�ǲ���Ҫ�ģ�Ĭ�Ͼ���ָ��Ŀ��classpath·�����棨src���棩�����Ҫʹ�þ��ClassPathXmlApplicationContext()·������Ҫ����file: ǰ׺��ʾ���Ǿ��·����
	����FileSystemXmlApplicationContext������Ĭ�ϱ�ʾ����}�֣�
	1��û���̷������Ŀ����·���� ����Ŀ�ĸ�Ŀ¼��
	2�����̷��ʾ�����ļ����·����
	3�����Ҫʹ��classpath·������Ҫǰ׺classpath:  
	���磺
	��ClassPathXmlApplicationContext()��
	1��ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	2��ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
	3��ApplicationContext factory=new ClassPathXmlApplicationContext("file:D:\��ѧѧϰ\����\ʵ���ҵ�����\workspace\SpringTest\src\applicationContext.xml");
	��FileSystemXmlApplicationContext������
	1�� ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");    
	2��ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
	3��ApplicationContext factory=new FileSystemXmlApplicationContext("file:D:\��ѧѧϰ\����\ʵ���ҵ�����\workspace\SpringTest\src\applicationContext.xml");
	4��ApplicationContext factory=new FileSystemXmlApplicationContext("D:\��ѧѧϰ\����\ʵ���ҵ�����\workspace\SpringTest\src\applicationContext.xml");

	 */
	static {
		System.out.println("*************_*****************");
		//        _ctx = new FileSystemXmlApplicationContext("I:\\MyEclipse6\\WorkSpace\\mengniu\\WebRoot\\WEB-INF\\config\\base-config.xml"); 
		_ctx = new FileSystemXmlApplicationContext("classpath:base-config.xml");
		//        _ctx = new ClassPathXmlApplicationContext("base-config.xml"); 
		if (_ctx != null) {
			System.out.println("*************_ctx OK*****************");
		} else {
			System.out.println("*************_ctx fail*****************");
		}

//		try {
//			SellThread st = new SellThread();
//			new Thread(st).start();
//		} catch (Exception e) {
//
//		}
	}

	public ContextHelper() {
	}

	public static FileSystemXmlApplicationContext getContext() {
		return _ctx;
	}

}

class SellThread implements Runnable {
	public SellThread() {

	}

	cn.com.elite.workorder.WorkOrderBean informationBean = null;
	int icount = 0;

	public void run() {
		DataSource dataSource = null;

		try {
			dataSource = (DataSource) ContextHelper.getContext().getBean(
					"dataSource");

			informationBean = new cn.com.elite.workorder.WorkOrderBean();
			informationBean.setDataSource(dataSource);

		} catch (Exception e) {
			e.printStackTrace();
		}

		while (true) { //Param.Run_flag) {
			System.out.println("----------线程挂起5秒 ------------");
			//log.info("----------���̹߳���7��  ------------"); 
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String sysdate = "";
			try {
				List list = informationBean.getDealSMS();

				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);

					String DocumentOut_GUID = cn.com.elite.util.Util
							.filterForObject(map.get("DocumentOut_GUID"));
					String DocumentPath = cn.com.elite.util.Util
							.filterForObject(map.get("DocumentPath"));
					String DocumentTo = cn.com.elite.util.Util
							.filterForObject(map.get("DocumentTo"));
					String DocumentFrom = cn.com.elite.util.Util
							.filterForObject(map.get("DocumentFrom"));
					
					sendSMS(DocumentOut_GUID, DocumentPath, DocumentTo, informationBean);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}

	public void sendSMS(String DocumentOut_GUID, String message,
			String DocumentTo, cn.com.elite.workorder.WorkOrderBean informationBean) {
		System.out.println("---message=" + message);
		String url = "http://127.0.0.1:8080/SMS/index.jsp?user=";
		url = WORKORDERFRAME.SENDSMSURL + "?user="
				+ java.net.URLEncoder.encode(cn.com.elite.workorder.WORKORDERFRAME.USER) + "&pwd=" + cn.com.elite.workorder.WORKORDERFRAME.PASSWD + "&mobile="
				+ DocumentTo + "&message=" + java.net.URLEncoder.encode(message)
				+ "&msgid=" + DocumentOut_GUID;
		System.out.println("---url=" + url);
		URLConnection conn = null;
		OutputStreamWriter out = null;
		try {
			URL realUrl = new URL(url);
			conn = realUrl.openConnection();
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			conn.connect();

			String result = "";
			BufferedReader in = new BufferedReader(new InputStreamReader(conn
					.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += "\n" + line;
			}
			in.close();
			System.out.println("result=" + result);
			System.out.println("result.length=" + result.length());
			String rst = "0";
			if (result.equals("0")) {
				//���ŷ��ͳɹ�����ʱȡ����״̬
//				Thread.sleep(Long.valueOf(WORKORDERFRAME.QUERYSMSSTATUSTIME));
//				int icount = Integer
//						.parseInt(WORKORDERFRAME.QUERYSMSSTATUSCOUNT);
//				for (int i = 0; i < icount; i++) {
//					String sendresult = inquerystatus(cn.com.elite.workorder.WORKORDERFRAME.USER, cn.com.elite.workorder.WORKORDERFRAME.PASSWD, DocumentOut_GUID);
//					result = sendresult;
//					System.out.println("��ѯ����״̬���ؽ��=" + sendresult);
//					if (sendresult != null) {
//						if (!sendresult.equals("") && !sendresult.equals("500")
//								&& !sendresult.equals("501")
//								&& !sendresult.equals("550")
//								&& !sendresult.equals("600")) {
//
//							int ret = 0;
//							rst = "1";
//							ret = informationBean.updateDocumentout(DocumentOut_GUID, "1");
//							break;
//						} else {
//							int ret = 0;
//							//ret = informationBean.updateDocumentout(DocumentOut_GUID, "2");
//						}
//					}
//				}
			}
			if(result.trim().equals("0")){
				informationBean.updateDocumentoutFail(DocumentOut_GUID, "1");
			} else {
				informationBean.updateDocumentout(DocumentOut_GUID, "5");
			}
				
			

		} catch (Exception e) {
			System.out.println("发送GET请求失败：" + e);
			try {
				informationBean.updateDocumentout(DocumentOut_GUID, "5");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public String inquerystatus(String user, String pwd, String msgid)
			throws Exception {

		/*
		 ����	Ҫ��	����	��ע
		 user	��ѡ	�˺���	�ʺţ���Ҫ����
		 pwd	��ѡ	����	��¼����
		 msgid	��ѡ	��Ϣ��ʶ	�Զ��壬���ڶ�ȡ��Ϣ״̬����Ϊ������Ϊ����Ҫ״̬����
		 ����45���ַ����ڣ�ʱ��Ϳ���
		 */

		String result = "";

		System.out.println("---user=" + user);
		String url = "http://127.0.0.1:8080/SMS/index.jsp?user=";
		url = WORKORDERFRAME.QUERYSTATUSURL + "?user=" + user + "&pwd=" + pwd
				+ "&msgid=" + msgid;
		//		String param = "'1'";
		//		param = java.net.URLEncoder.encode(param);
		//		url = url+param;
		System.out.println("---url=" + url);
		URLConnection conn = null;
		OutputStreamWriter out = null;
		try {
			URL realUrl = new URL(url);
			conn = realUrl.openConnection();
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			conn.connect();

			BufferedReader in = new BufferedReader(new InputStreamReader(conn
					.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += "\n" + line;
			}
			in.close();
			System.out.println("result=" + result);
			//result = "500";

		} catch (Exception e) {
			System.out.println("����GET��������쳣��" + e);
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		return result;
	}
}
